<?php
/**
 * 股东系统
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/10/25
 * Time: 11:24
 */
namespace app\admin\controller;


use app\admin\service\BannerService;
use app\admin\service\UploadService;
use think\Db;
use think\log;
use think\Loader;

class Banner extends Base
{
    public function lists(){
        $bannerService = new BannerService();
        if($this->request->isAjax()){
            $id = intval(input('post.id',0));
            if(empty($id)){
                ret('','参数错误');
            }
            $banner = $bannerService->findBannerById($id);
            if(empty($banner)){
                ret('','banner不存在');
            }
            $re = $bannerService->deleteById($id);
            if(empty($id)){
                ret('','banner删除失败');
            }
            ret('','删除成功',TRUE);
        }
        $page_data['page'] = intval(input('page',1)) < 1 ? 1 : intval(input('page',1));
        $page_data['page_size'] = 20;
        $order = 'id desc';
        $where = [];
        $list = $bannerService->getBannerList($where,$order,$page_data);
        $this->assign([
            'list' => $list
        ]);
        return $this->fetch();
    }

    public function add(){
        if($this->request->isAjax())
        {
            $data['title'] = input('post.title','');
            $data['link'] = input('post.link','');
            $data['desc'] = input('post.desc','');
            $data['img'] = input('post.img','');
            $data['status'] = input('post.status','');
            foreach ($data as $val){
                if(empty($val)){
                    ret('','参数错误');
                }
            }
            $bannerService = new BannerService();
            $re = $bannerService->createBanner($data);
            if($re){
                return  ret('','添加成功',TRUE);
            }
            return  ret('','添加失败',FALSE);
        }

        return $this->fetch();
    }

    public function upload(){
        $image = $this->request->file('file');
        if(empty($image)){
            ret('','请传入图像');
        }
        $uploadService = new UploadService();
        $re = $uploadService->saveUploadFileImage($image);
        if($re['code'] !== 0){
            ret($re['msg'],'储存图像失败');
        }
        ret($re['data'],'上传成功',TRUE);
    }

    public function upload_base64(){
        $image = input('file','');
        if(empty($image)){
            ret('','请传入图像');
        }

        $uploadService = new UploadService();
        $re = $uploadService->saveUploadBase64Image($image);
        if($re['code'] !== 0){
            ret($re['msg'],'储存图像失败');
        }
        ret($re,'上传成功',TRUE);
    }
}